// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// A [author] rhyme_lph
// Q [email]  rhymelph@gmail.com
// B [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/a.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/report_list_item_entity.dart';
import 'package:finance_app/src/entities/tax_submission_detail_entity.dart';
import 'package:finance_app/src/utils/cash_utils.dart';
import 'package:finance_app/src/utils/date_time_utils.dart';
import 'package:finance_app/src/utils/form_utils.dart';
import 'package:finance_app/src/utils/language_utils.dart';
import 'package:finance_app/src/utils/report_utils.dart';
import 'package:finance_app/src/widgets/button/filter_button.dart';
import 'package:finance_app/src/widgets/button/normal_float_svg_button.dart';
import 'package:finance_app/src/widgets/button/normal_float_text_button.dart';
import 'package:finance_app/src/widgets/button/normal_float_text_white_button.dart';
import 'package:finance_app/src/widgets/button/normal_popup_button.dart';
import 'package:finance_app/src/widgets/chip/normal_chip.dart';
import 'package:finance_app/src/widgets/common/normal_table_pro.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/form/form_edit_status_inherited_widget.dart';
import 'package:finance_app/src/widgets/form/status_line_form.dart';
import 'package:finance_app/src/widgets/input/search_input_field.dart';
import 'package:finance_app/src/widgets/list_item/time_select_item.dart';
import 'package:finance_app/src/widgets/list_item/value_border_Item.dart';
import 'package:finance_app/src/widgets/pop/normal_border_pop.dart';
import 'package:finance_app/src/widgets/pop/normal_title_border_pop.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/all_reports/date_report_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:flutter/services.dart';
import 'package:flutter_sticky_header/flutter_sticky_header.dart';
import 'package:open_file/open_file.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:tuple/tuple.dart';
import 'package:url_launcher/url_launcher.dart';

export 'package:finance_app/src/provider/all_reports/date_report_provider.dart';
export 'package:finance_app/src/entities/tax_submission_detail_entity.dart';

@RouterMeta(paramName: "dateReportPage")
class DateReportPage extends StatefulWidget {
  final int type; //类型
  final Map meta;
  final TaxSubmissionDetailTaxSubmissionLines line;
  final List<TaxSubmissionDetailTaxSubmissionLines> lines;
  final List<TaxSubmissionDetailEntity> taxes;
  final TaxSubmissionDetailEntity tax;
  final bool acceptMode;
  final bool canEdit;

  const DateReportPage({
    Key key,
    this.type,
    this.acceptMode,
    this.line,
    this.meta,
    this.lines,
    this.taxes,
    this.tax,
    this.canEdit,
  }) : super(key: key);

  @override
  _DateReportPageState createState() => _DateReportPageState();
}

class _DateReportPageState extends State<DateReportPage>
    with PagePresenter<DateReportPage, DateReportProvider> {
  Widget buildTableHeader(BuildContext context,
      {List<String> headerItemList,
      EdgeInsets padding,
      bool hasDivider,
      List<int> widthList}) {
    if (widget.type > 20 && widget.type < 30) {
      return NormalTableProAgeHeader(
        items: headerItemList,
        padding: padding,
        hasDivider: hasDivider,
        widthList: widthList,
      );
    }
    return NormalTableProHeader(
      items: headerItemList,
      widthList: widthList,
    );
  }

  String buildTitle() {
    if (widget.type == 3 || widget.type == 6) {
      return widget.meta['contact_name'];
    }
    if (widget.type == 61 && widget.canEdit == true) {
      return "Edit Balance";
    }
    if (widget.type == 76) {
      return "Reconciliation Report";
    }
    return ReportUtils.reportToType.entries
        .firstWhere((element) => element.value == widget.type,
            orElse: () => null)
        ?.key;
  }

  @override
  PreferredSizeWidget get appBar => getDefaultAppBar(
          title: LanguageUtils.translateReport(buildTitle()),
          actions: [
            if (provider.menuList.length != 0)
              listBuilder<Tuple2<List<String>, bool>>(
                selector: (_, __) => Tuple2<List<String>, bool>(
                    List.from(__.menuList), __.editMode),
                builder: (BuildContext context,
                        Tuple2<List<String>, bool> value, Widget child) =>
                    value.item1.length == 0 || value.item2
                        ? Container()
                        : NormalMorePopupButton(
                            onSelect: provider.onSelectMenu,
                            menuList: provider.menuList,
                          ),
              )
          ]);

  @override
  Widget get buildWidget => Stack(
        children: [
          _buildList(),
          _buildBottomBar(),
          _buildAcceptBottomBar(),
          _buildEditBalanceBottomBar(),
          _buildCancelSaveBar(),
          _buildAttachmentBottomBar(),
        ],
      );

  Widget _buildAttachmentBottomBar() => Positioned(
        left: 40.0,
        bottom: MediaQuery.of(context).padding.bottom + getDp(40),
        child: Visibility(
          visible: provider.data.attachments != null &&
              provider.data.attachments.length > 0,
          child: PressedColorButton(
            padding: EdgeInsets.all(12),
            onPressed: () {
              launch(provider.data.attachments.first.url);
            },
            child: Icon(
              CupertinoIcons.link,
              size: getDp(27),
              color: aceBlueColor,
            ),
          ),
        ),
      );

  Widget _buildAcceptBottomBar() => Positioned(
        left: 0.0,
        right: 0.0,
        bottom: MediaQuery.of(context).padding.bottom + getDp(40),
        child: Center(
          child: Visibility(
            visible: widget.type == 61 && widget.acceptMode == true,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                NormalFloatTextWhiteButton(
                  title: S.current.cancel,
                  onTap: provider.onCancelAccept,
                ),
                SizedBox(
                  width: getDp(15),
                ),
                NormalFloatTextButton(
                  title: S.current.accept,
                  onTap: provider.onAccept,
                ),
              ],
            ),
          ),
        ),
      );

  Widget _buildBottomBar() => listBuilder<bool>(
      selector: (_, __) => __.buttonBarIsUp,
      builder: (BuildContext context, bool value, Widget child) =>
          AnimatedPositioned(
              duration: Duration(milliseconds: 300),
              left: 0.0,
              right: 0.0,
              bottom: value
                  ? MediaQuery.of(context).padding.bottom + getDp(40)
                  : -getDp(60),
              child: Center(
                child: Visibility(
                  visible: widget.type == 75 || widget.type == 77,
                  child: NormalFloatSvgButton(
                    onTap: provider.onNavAdd,
                    asset: A.assetsImagesDashboardAdd,
                  ),
                ),
              )));

  Widget _buildCancelSaveBar() => listBuilder<bool>(
        selector: (_, __) => __.editMode,
        builder: (BuildContext context, bool value, Widget child) =>
            AnimatedPositioned(
          duration: Duration(milliseconds: 300),
          bottom: value
              ? getDp(32) + MediaQuery.of(context).padding.bottom
              : -getDp(60),
          left: 0.0,
          right: 0.0,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              NormalFloatTextWhiteButton(
                title: S.current.cancel,
                onTap: provider.onCancelEdit,
              ),
              SizedBox(
                width: getDp(15),
              ),
              NormalFloatTextButton(
                title: S.current.save,
                onTap: provider.onSave,
              ),
            ],
          ),
        ),
      );

  Widget _buildList() => ColoredBox(
        color: Colors.white,
        child: listBuilder<List<ReportListItemEntity>>(
          selector: (_, __) => List.from(__.reportList),
          builder: (BuildContext context, List<ReportListItemEntity> value,
                  Widget child) =>
              listBuilder<Tuple2<bool, int>>(
            selector: (_, __) => Tuple2(__.hasUp, __.data.body.length),
            builder:
                (BuildContext context, Tuple2<bool, int> value, Widget child) =>
                    SmartRefresher(
              controller: provider.refreshController,
              onRefresh: provider.onRefresh,
              onLoading: provider.onLoadMore,
              enablePullUp: false,
              child: CustomScrollView(
                controller: provider.controller,
                slivers: [
                  _buildMonthSelect(),
                  _buildBankView(),
                  _buildClosingDate(),
                  _buildAsAtSelect(),
                  _buildTopFilterList(),
                  _buildTaxPeriodSelect(),
                  _buildLineSelect(),
                  _buildStatusLine(),
                  _buildAccountPeriod(),
                  _buildYearOfAssessment(),
                  _buildTotalTable(),
                  _buildSearchInput(),
                  _buildDateTimeSelect(),
                  _buildFilterColumn(),
                  _buildFilterList(),
                  ..._buildDataList(),
                ],
              ),
            ),
          ),
        ),
      );

  // SliverStickyHeader(
  //   header: buildTableHeader(context),
  //   sliver: SliverList(
  //       delegate: SliverChildListDelegate(buildTableItem(context))),
  // ),

  List<Widget> _buildDataList() {
    List<Widget> children = [];
    Widget header = Container();
    if (widget.type == 21 || widget.type == 22) {
      header = buildTableHeader(context, hasDivider: false, headerItemList: [
        S.current.current_,
        "1 - 30",
        "31 - 60",
        "61+",
      ]);
    }
    bool hasHeader = false;
    List<int> widthList;
    for (int index = 0; index < provider.reportList.length; index++) {
      final item = provider.reportList[index];
      if (item.style == 'heading') {
        if (hasHeader != true && index == 0) {
          if (item.meta != null &&
              item.meta is Map &&
              item.meta['data'] != null &&
              item.meta['data'] is Map) {
            widthList = (item.meta['data']['width'] as List).cast<int>();
          }
          header = buildTableHeader(context,
              headerItemList: item.values,
              hasDivider: false,
              widthList: widthList);
          hasHeader = true;
        } else {
          children.add(PressedOpacityButton(
            onPressed: () => provider.onNavReportDetail(item),
            child: NormalTableProSubTitleItem(
              title: item.values[0],
              value: item.values.length > 1 ? item.values[1] : null,
              onUpdateValue: item.meta is Map &&
                      item.meta['goto_id'] != null &&
                      widget.canEdit == true
                  ? (data) => provider.onUpdateValue(item, data)
                  : null,
            ),
          ));
        }
      } else if (item.style == 'normal' || item.style == 'normal_compact') {
        if ((widget.type == 21 || widget.type == 22) && item.column == 2) {
          children.add(PressedOpacityButton(
            onPressed: () => provider.onNavAgeDetail(item.meta),
            child: NormalTableProSubTitleNormalItem(
              title: item.values[0],
              value: item.values[1],
            ),
          ));
        } else if (widget.type == 2 || widget.type == 5) {
          children.add(PressedOpacityButton(
            onPressed: () => provider.onNavCompanyDetail(item),
            child: NormalTableProItem(
              items: item.values,
              widthList: widthList,
            ),
          ));
        } else if (widget.type == 51) {
          children.add(PressedOpacityButton(
            onPressed: provider.editMode
                ? null
                : () => provider.onNavGSTLineDetail(item),
            child: NormalTableProItem(
              items: item.values,
              widthList: widthList,
              onUpdateValue: provider.editMode &&
                      (item.values[0] == "9" ||
                          item.values[0] == "10" ||
                          item.values[0] == "11" ||
                          item.values[0] == "12" ||
                          item.values[0] == "14" ||
                          item.values[0] == "15" ||
                          item.values[0] == "17" ||
                          item.values[0] == "19")
                  ? (data) => provider.onUpdateLineValue(item, data)
                  : null,
            ),
          ));
        } else if (widget.type == 41) {
          children.add(PressedOpacityButton(
            onPressed: () => provider.onNavInventoryDetail(item),
            child: NormalTableProItem(
              items: item.values,
              widthList: widthList,
            ),
          ));
        } else {
          children.add(PressedOpacityButton(
            onPressed: () => provider.onNavReportDetail(item),
            child: NormalTableProItem(
              items: item.values,
              widthList: widthList,
              padding: item.style == 'normal_compact'
                  ? EdgeInsets.only(
                      left: getDp(35),
                      top: provider.reportList.length > index
                          ? provider.reportList[index - 1].style != item.style
                              ? 10
                              : 0
                          : 0,
                      right: getDp(35),
                      bottom: provider.reportList.length > index + 1
                          ? provider.reportList[index + 1].style == item.style
                              ? 10 // space between compacted item
                              : 10
                          : 10, //last item
                    )
                  : null,
              onUpdateValue: item.meta is Map &&
                      item.meta['goto_id'] != null &&
                      widget.canEdit == true
                  ? (data) => provider.onUpdateValue(item, data)
                  : null,
            ),
          ));
        }
      } else if (item.style == 'break') {
        children.add(AppSpaces.grayH05);
      } else if (item.style == 'break_large') {
        String background;
        if (item.meta != null &&
            item.meta is Map &&
            item.meta['data'] != null &&
            item.meta['data'] is Map &&
            item.meta['data']['background'] != null) {
          background = item.meta['data']['background'];
          background = background.replaceAll('#', '0xFF');
        }
        if (background != null) {
          children.add(Container(
            height: getDp(20),
            color: background != null
                ? Color(int.tryParse(background))
                : aceGrayColor,
          ));
        } else {
          children.add(AppSpaces.grayH6);
        }
      } else if (item.style == 'background') {
        if (widget.type == 75) {
          children.add(PressedOpacityButton(
            onPressed: () => provider.onNavReportDetail(item),
            child: NormalTableProTitleItem(
              title: item.values[0],
              value: item.values.length > 1 ? item.values[1] : null,
            ),
          ));
        } else {
          //Header
          if (item.values.length > 0)
            children.add(NormalTableProTitleItem(
              title: item.values[0],
              padding: widget.type == 31 || widget.type == 32
                  ? EdgeInsets.only(
                      left: getDp(25),
                      top: getDp(5),
                      right: getDp(35),
                      bottom: getDp(5),
                    )
                  : null,
              value: item.values.length > 1 ? item.values[1] : null,
              onUpdateValue: item.meta is Map &&
                      item.meta['goto_id'] != null &&
                      widget.canEdit == true
                  ? (data) => provider.onUpdateValue(item, data)
                  : null,
            ));
        }
      } else if (item.style == 'bold' && item.column == 2) {
        children.add(NormalTableProTitleNoBackItem(
          title: item.values[0],
          value: item.values.length > 1 ? item.values[1] : null,
        ));
      } else if (item.style == 'bold' && item.column == 4) {
        children.add(NormalTableProTitleNoBackItem4(
          items: item.values,
        ));
      } else if (item.style == 'bold' &&
          (item.column == 3 || item.column == 1)) {
        children.add(NormalTableProTitleNoBackItem4(
          items: item.values,
          widthList: widthList,
        ));
      }
    }
    children.add(AppSpaces.h90);
    return [
      SliverStickyHeader(
        header: header,
        sliver: SliverList(
            delegate: SliverChildBuilderDelegate(
                (BuildContext context, int index) => children[index],
                childCount: children.length)),
      )
    ];
  }

  Widget _buildFormToDateWidget() {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: getDp(35)),
      child: Row(
        children: [
          Expanded(
            child: listBuilder<DateTime>(
              selector: (_, __) => __.from,
              builder: (BuildContext context, DateTime value, Widget child) =>
                  TimeSelectItem(
                      title: S.current.from,
                      value: value == null
                          ? Config.settingInfo.dateFormat.toUpperCase() ??
                              "DD MMM YYYY"
                          : DateTimeUtils.formatGlobalDateTime(value),
                      onTap: provider.onSelectFrom),
            ),
          ),
          SizedBox(
            width: getDp(13),
          ),
          Expanded(
            child: listBuilder<DateTime>(
              selector: (_, __) => __.to,
              builder: (BuildContext context, DateTime value, Widget child) =>
                  TimeSelectItem(
                      title: S.current.to,
                      value: value == null
                          ? Config.settingInfo.dateFormat.toUpperCase() ??
                              "DD MMM YYYY"
                          : DateTimeUtils.formatGlobalDateTime(value),
                      onTap: provider.onSelectTo),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildTopFilterList() {
    return SliverToBoxAdapter(
      child: Visibility(
        visible: widget.type == 41,
        child: listBuilder<int>(
            builder: (BuildContext context, int value, Widget child) =>
                value == 0
                    ? Container()
                    : Container(
                        height: getDp(30),
                        margin: EdgeInsets.only(
                            top: getDp(10),
                            bottom: getDp(10),
                            left: getDp(20),
                            right: getDp(20)),
                        alignment: Alignment.centerLeft,
                        child: ListView.separated(
                          padding: EdgeInsets.symmetric(
                            horizontal: getDp(10),
                          ),
                          scrollDirection: Axis.horizontal,
                          shrinkWrap: true,
                          itemBuilder: (BuildContext context, int index) =>
                              _buildFilterItem(context, index),
                          itemCount: value,
                          separatorBuilder: (BuildContext context, int index) {
                            return Container(
                              width: getDp(15),
                            );
                          },
                        ),
                      ),
            selector: (_, __) => __.currentSelectFilterList.length),
      ),
    );
  }

  Widget _buildFilterList() {
    return SliverToBoxAdapter(
      child: Visibility(
        visible: (widget.type > 10 && widget.type < 20) ||
            (widget.type > 30 && widget.type < 40),
        child: listBuilder<int>(
            builder: (BuildContext context, int value, Widget child) =>
                value == 0
                    ? Container()
                    : Container(
                        height: getDp(30),
                        margin: EdgeInsets.only(
                            top: getDp(10),
                            bottom: getDp(10),
                            left: getDp(20),
                            right: getDp(20)),
                        alignment: Alignment.centerLeft,
                        child: ListView.separated(
                          padding: EdgeInsets.symmetric(
                            horizontal: getDp(10),
                          ),
                          scrollDirection: Axis.horizontal,
                          shrinkWrap: true,
                          itemBuilder: (BuildContext context, int index) =>
                              _buildFilterItem(context, index),
                          itemCount: value,
                          separatorBuilder: (BuildContext context, int index) {
                            return Container(
                              width: getDp(15),
                            );
                          },
                        ),
                      ),
            selector: (_, __) => __.currentSelectFilterList.length),
      ),
    );
  }

  Widget _buildFilterItem(BuildContext context, int index) {
    return listBuilder<String>(
        builder: (BuildContext context, String value, Widget child) =>
            NormalChip(
              title: LanguageUtils.translateFilter(value),
              onClose: () => provider.onRemoveFilter(value),
            ),
        selector: (_, __) => __.currentSelectFilterList[index]);
  }

  Widget _buildFilterColumn() => SliverToBoxAdapter(
        child: Visibility(
          visible: widget.type > 10 && widget.type < 20,
          child: Container(
            width: MediaQuery.of(context).size.width,
            alignment: Alignment.centerRight,
            child: FilterButton(
              horizontalPadding: getDp(35),
              onPressed: provider.onShowFilter,
            ),
          ),
        ),
      );

  Widget _buildDateTimeSelect() => SliverToBoxAdapter(
        child: Visibility(
            visible: (widget.type > 0 && widget.type < 20) ||
                widget.type == 42 ||
                widget.type == 71 ||
                widget.type == 72 ||
                widget.type == 73 ||
                widget.type == 74 ||
                widget.type == 75 ||
                widget.type == ReportUtils.GeneralLedgerDetail,
            child: Column(
              children: [
                AppSpaces.h20,
                listBuilder<String>(
                  selector: (_, __) => __.currentSelectDatePop,
                  builder: (BuildContext context, String value, Widget child) =>
                      NormalBorderPop<String>(
                    select: value,
                    buildDisplayName: (e) => e,
                    group: provider.datePopList,
                    onSelect: provider.onChangeDate,
                  ),
                ),
                AppSpaces.h15,
                _buildFormToDateWidget(),
                AppSpaces.h20,
              ],
            )),
      );

  _buildTotalTable() => SliverToBoxAdapter(
        child: Visibility(
            visible: widget.type > 20 && widget.type < 30,
            child: Container(
              margin: EdgeInsets.symmetric(
                horizontal: getDp(16),
                vertical: getDp(10),
              ),
              decoration: BoxDecoration(
                  border: Border.all(
                    color: aceWhiteGreyColor,
                    width: getDp(1.5),
                  ),
                  borderRadius: BorderRadius.circular(getDp(5))),
              child: Column(
                children: [
                  listBuilder<String>(
                    selector: (_, __) =>
                        CashUtils.formatCashAmount(__.data.total.total),
                    builder:
                        (BuildContext context, String value, Widget child) =>
                            NormalTableProSubTitleItem(
                      title: S.current.total,
                      value: "${Config.defaultCurrency.key} $value",
                      padding: EdgeInsets.symmetric(
                          horizontal: getDp(18), vertical: getDp(15)),
                    ),
                  ),
                  AppSpaces.grayH05,
                  buildTableHeader(context,
                      padding: EdgeInsets.only(
                        left: getDp(19),
                        right: getDp(24),
                        top: getDp(10),
                      ),
                      hasDivider: false,
                      headerItemList: [
                        S.current.current_,
                        "1 - 30",
                        "31 - 60",
                        "61+",
                      ]),
                  NormalTableProHighLightItem(
                    items: [
                      CashUtils.formatCashAmount(provider.data?.total?.current),
                      CashUtils.formatCashAmount(provider.data?.total?.days30),
                      CashUtils.formatCashAmount(provider.data?.total?.days60),
                      CashUtils.formatCashAmount(provider.data?.total?.days61)
                    ],
                    padding: EdgeInsets.only(
                        left: getDp(16),
                        right: getDp(24),
                        top: getDp(4),
                        bottom: getDp(15)),
                    hasDivider: false,
                  )
                ],
              ),
            )),
      );

  Widget _buildAsAtSelect() => SliverToBoxAdapter(
        child: Visibility(
            visible: (widget.type > 20 && widget.type < 30) ||
                (widget.type > 30 && widget.type < 40) ||
                widget.type == 41 ||
                widget.type == 77 ||
                widget.type == 78,
            child: Padding(
              padding: EdgeInsets.only(
                  top: getDp(20),
                  left: getDp(35),
                  right: widget.type > 30 && widget.type < 40 ? 0 : getDp(35),
                  bottom: getDp(10)),
              child: Row(
                children: [
                  Expanded(
                    child: listBuilder<DateTime>(
                      selector: (_, __) => __.asAt,
                      builder: (BuildContext context, DateTime value,
                              Widget child) =>
                          TimeSelectItem(
                              title: S.current.as_at,
                              value: value == null
                                  ? Config.settingInfo.dateFormat
                                          .toUpperCase() ??
                                      "DD MMM YYYY"
                                  : DateTimeUtils.formatGlobalDateTime(value),
                              onTap: widget.type == 77 || widget.type == 78
                                  ? null
                                  : provider.onSelectAsAt),
                    ),
                  ),
                  Visibility(
                      visible: (widget.type > 30 && widget.type < 40) ||
                          widget.type == 41,
                      child: FilterButton(
                        onPressed: provider.onShowFilter,
                      )),
                ],
              ),
            )),
      );

  Widget _buildSearchInput() => SliverToBoxAdapter(
        child: Visibility(
            visible:
                (widget.type > 20 && widget.type < 30) || widget.type == 41,
            child: Padding(
              padding: EdgeInsets.only(
                top: getDp(10),
                bottom: getDp(10),
              ),
              child: SearchInputField(
                tip: widget.type == 41 ? S.current.search_by_name_code : null,
                controller: provider.searchController,
                focusNode: provider.searchFocusNode,
              ),
            )),
      );

  Widget _buildTaxPeriodSelect() => SliverToBoxAdapter(
        child: Visibility(
            visible:
                widget.type == 51 || widget.type == 52 || widget.type == 53,
            child: listBuilder<String>(
              selector: (_, __) => __.currentSelectTax.timeRange,
              builder: (BuildContext context, String value, Widget child) =>
                  Padding(
                padding: EdgeInsets.only(top: getDp(20)),
                child: NormalTitleBorderPop(
                  group: provider.taxSubmissions
                      .map((e) =>
                          '${DateTimeUtils.formatDateTimeString(e?.startDate)} - ${DateTimeUtils.formatDateTimeString(e?.endDate)}')
                      .toList(),
                  select: value,
                  title: S.current.tax_period,
                  onSelect:
                      provider.editMode ? null : provider.onChangeSubmission,
                ),
              ),
            )),
      );

  Widget _buildLineSelect() => SliverToBoxAdapter(
        child: Visibility(
            visible: widget.type == 53,
            child: listBuilder<String>(
              selector: (_, __) => __.line.line.toString(),
              builder: (BuildContext context, String value, Widget child) =>
                  Padding(
                padding: EdgeInsets.only(top: getDp(20)),
                child: NormalTitleBorderPop(
                  group: provider.lines.map((e) => '${e.line}').toList(),
                  select: value,
                  title: S.current.line,
                  onSelect: provider.onChangeLine,
                ),
              ),
            )),
      );

  Widget _buildStatusLine() => SliverToBoxAdapter(
        child: Visibility(
            visible: widget.type == 51,
            child: Padding(
              padding: EdgeInsets.only(
                top: getDp(20),
              ),
              child: listBuilder<String>(
                selector: (_, __) => __.currentSelectTax?.status,
                builder: (BuildContext context, String value, Widget child) =>
                    FormEditStatusInheritedWidget(
                  isEdit: false,
                  child: StatusLineForm(
                    status: value,
                    model: ReimburseModel.gstF5,
                  ),
                ),
              ),
            )),
      );

  Widget _buildAccountPeriod() => SliverToBoxAdapter(
        child: Visibility(
            visible: widget.type == 61,
            child: Padding(
              padding: EdgeInsets.only(
                top: getDp(20),
              ),
              child: listBuilder<String>(
                selector: (_, __) => __.currentSelectTax.timeRange,
                builder: (BuildContext context, String value, Widget child) =>
                    ValueBorderItem(
                  title: S.current.accounting_period,
                  value: value,
                ),
              ),
            )),
      );

  _buildYearOfAssessment() => SliverToBoxAdapter(
        child: Visibility(
            visible: widget.type == 61,
            child: Padding(
              padding: EdgeInsets.only(
                top: getDp(20),
                bottom: getDp(20),
              ),
              child: listBuilder<String>(
                selector: (_, __) => __.yearOfAssessment,
                builder: (BuildContext context, String value, Widget child) =>
                    ValueBorderItem(
                  title: S.current.year_of_assessment,
                  value: value,
                ),
              ),
            )),
      );

  @override
  void initState() {
    provider.initDate(widget.type, widget.meta, widget.line, widget.lines,
        widget.taxes, widget.tax, widget.canEdit);
    super.initState();
  }

  Widget _buildBankView() => SliverToBoxAdapter(
        child: Visibility(
            visible: widget.type == 76,
            child: Padding(
              padding: EdgeInsets.only(
                  top: getDp(20),
                  left: getDp(35),
                  right: getDp(35),
                  bottom: getDp(10)),
              child: listBuilder<DateTime>(
                selector: (_, __) => __.asAt,
                builder: (BuildContext context, DateTime value, Widget child) =>
                    TimeSelectItem(
                  title: null,
                  value: widget.meta['bankName'],
                ),
              ),
            )),
      );

  Widget _buildClosingDate() => SliverToBoxAdapter(
        child: Visibility(
            visible: widget.type == 76,
            child: Padding(
              padding: EdgeInsets.only(
                  top: getDp(20),
                  left: getDp(35),
                  right: getDp(35),
                  bottom: getDp(10)),
              child: listBuilder<DateTime>(
                selector: (_, __) => __.asAt,
                builder: (BuildContext context, DateTime value, Widget child) =>
                    TimeSelectItem(
                  title: S.current.closing_date,
                  value: DateTimeUtils.formatDateTimeString(
                      widget.meta['closingDate']),
                ),
              ),
            )),
      );

  _buildEditBalanceBottomBar() => Positioned(
        left: 0.0,
        right: 0.0,
        bottom: MediaQuery.of(context).padding.bottom + getDp(40),
        child: Center(
          child: Visibility(
            visible: widget.type == 61 &&
                widget.canEdit == true &&
                MediaQuery.of(context).viewInsets.bottom == 0.0,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                NormalFloatTextWhiteButton(
                  title: S.current.cancel,
                  onTap: provider.onCancelAccept,
                ),
                SizedBox(
                  width: getDp(15),
                ),
                NormalFloatTextButton(
                  title: S.current.save,
                  onTap: provider.onSave,
                ),
              ],
            ),
          ),
        ),
      );

  Widget _buildMonthSelect() => SliverToBoxAdapter(
        child: Visibility(
            visible: widget.type == ReportUtils.payrollMonthlySummary,
            child: Padding(
              padding: EdgeInsets.only(top: getDp(20), bottom: getDp(10)),
              child: listBuilder<DateTime>(
                selector: (_, __) => __.month,
                builder: (BuildContext context, DateTime value, Widget child) =>
                    NormalBorderPop<DateTime>(
                  select: value,
                  buildDisplayName: (e) =>
                      DateTimeUtils.formatDateTimeMMMYYYY(e) ?? '',
                  group: provider.monthList,
                  onSelect: provider.onChangeMonthDate,
                ),
              ),
            )),
      );
}
